package com.gxa.insurance.service.impl;

import com.gxa.insurance.mapper.BannerMapper;
import com.gxa.insurance.pojo.Admin;
import com.gxa.insurance.pojo.Banner;
import com.gxa.insurance.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Min
 */
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper mapper;

    @Override
    public Map<String,Object> selectAllByPage(String page, String pageSize) {
        Map<String,Object> map = new HashMap<>();
        // 转换
        int intPage=Integer.parseInt(page);
        int intPageSize=Integer.parseInt(pageSize);
        if(intPage<=0){
            intPage=1;
        }else if(intPageSize<=0){
            intPageSize=10;
        }
        intPage=(intPage-1)*intPageSize;
        // 分页查询
        List<Banner> admins = mapper.selectAllByPage(intPage, intPageSize);

        // 查询总条数
        Long count = mapper.selectCount();
        map.put("count",count);
        map.put("list",admins);
        return map;
    }

    @Override
    public Boolean addBanner(Banner banner) {
        List<Banner> banners=mapper.selectAllBanner();
        // 找出最大值
        Banner banner2 = banners.get(banners.size() - 1);
//        假设最大的为0
        int Max=banner2.getPiror();
        banner.setPiror(Max+1);
        mapper.insertBanner(banner);
        return true;
    }

    @Override
    public Boolean deleteBanner(String id, HttpServletRequest request) {
        Integer intId = Integer.parseInt(id);
        // 通过id查询出banner的地址
        Banner banner = mapper.selectById(intId);
        String cimage = banner.getCimage();
        String fileName = cimage.substring(cimage.lastIndexOf("/")+1);
        // 获取当先应用根路径
        String realPath = request.getSession().getServletContext().getRealPath("/");
        File file = new File(realPath);
        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(pathname.getName().equals(fileName)){
                    return true;
                }
                return false;
            }
        });

        // 删除文件
        for (File findFile : files) {
            findFile.delete();
        }
        mapper.deleteByID(intId);
        return true;
    }

    @Transactional
    @Override
    public Boolean exchangePriority(String id1, String id2) {
        int intId1 = Integer.parseInt(id1);
        int intId2=Integer.parseInt(id2);
        // 交换两个 banner的优先级
        Banner banner1 = mapper.selectById(intId1);
        Banner banner2 = mapper.selectById(intId2);
        int tempP= banner2.getPiror();
        banner2.setPiror(banner1.getPiror());
        banner1.setPiror(tempP);
        // 分别更新两个banner
        mapper.updatePriority(banner1);
        mapper.updatePriority(banner2);
        return  true;
    }

    @Override
    public Boolean updateById(Banner banner) {
        mapper.updateById(banner);
        return true;
    }

    @Override
    public Boolean updateEnableById(Integer id, Boolean isEnable) {
        mapper.updateEnableById(id,isEnable);
        return true;
    }

    @Override
    public Integer selectID(Integer prior) {
        Integer id=mapper.selectIdByPrior(prior);
        return id;
    }
}
